package com.hexinfo.dmpro.component.comparison.utils;

import cn.hutool.core.util.StrUtil;
import com.hexinfo.dmpro.component.common.util.DataSourceUtil;
import com.hexinfo.dmpro.component.comparison.constants.StreamStmtParamConstants;
import com.hexinfo.dmpro.component.comparison.entity.TableContrastParamEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Package com.hexinfo.dmpro.component.comparison.utils
 * @ClassName GetDataUtil
 * @Description sql查询工具类
 * @Author Huang Yanyihe
 * @Date 2023/9/10 22:29
 * @Version v1.0
 **/
@Component
@Slf4j
public class GetDataUtil {

    private Statement stmt = null;

    /**
     * @Method getDataCount
     * @Param conn
     * @Param dbTableName
     * @Param queryCriteria
     * @Return java.lang.Long
     * @Description 获取表数据总量
     * @Author Huang Yanyihe
     * @Date 2023/9/18 13:21
     * @Version V1.0
     */
    public long getDataCount(Connection conn, String dbTableName, String queryCriteria) {
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            String template = "select count(1) from {} {}";
            ResultSet rs = stmt.executeQuery(StrUtil.format(template, dbTableName, queryCriteria));
            rs.next();
            return rs.getLong(1);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtil.close(stmt);
        }
    }

    public void queryTableMetadata(Connection conn, String dbTableName) {

    }

    /**
     * @Method getStreamStmt
     * @Param conn
     * @Return java.sql.Statement
     * @Description 获取流式Statement
     * @Author Huang Yanyihe
     * @Date 2023/9/10 22:29
     * @Version V1.0
     */
    public void getStreamStmt(Connection conn) {
        try {

            this.stmt = conn.createStatement();
            this.stmt.setFetchSize(StreamStmtParamConstants.FETCH_SIZE);
            this.stmt.setQueryTimeout(StreamStmtParamConstants.QUERY_TIMEOUT);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Method getAllTargetQueryRs
     * @Param tableContrastParam
     * @Return java.sql.ResultSet
     * @Description 获取目标表查询结果
     * @Author Huang Yanyihe
     * @Date 2023/9/18 13:30
     * @Version V1.0
     */
    public ResultSet getAllTargetQueryRs(Connection connection, TableContrastParamEntity tableContrastParam) {
        try {
            //获取流式Statement
            getStreamStmt(connection);
            //查询数据
            String template = "select * from {} {}";
            String baseSql = StrUtil.format(template, tableContrastParam.getTargetDbAndTableName(), tableContrastParam.getTargetQueryCriteria());
            return stmt.executeQuery(baseSql);
        } catch (SQLException e) {
            log.error("【数据对比】-比对表:{}-查询异常:{}", tableContrastParam.getTableFlag(), e.getMessage(), e);
            throw new RuntimeException("目标表明细数据查询异常");
        }
    }

    /**
     * @Method getAllSourceQueryRs
     * @Param tableContrastParam
     * @Return java.sql.ResultSet
     * @Description 获取源表查询结果
     * @Author Huang Yanyihe
     * @Date 2023/9/18 13:31
     * @Version V1.0
     */
    public ResultSet getAllSourceQueryRs(Connection connection, TableContrastParamEntity tableContrastParam) {
        try {
            //获取流式Statement
            getStreamStmt(connection);
            //查询数据
            String template = "select * from  {} {}";
            String baseSql = StrUtil.format(template, tableContrastParam.getSourceDbAndTableName(), tableContrastParam.getSourceQueryCriteria());
            return stmt.executeQuery(baseSql);
        } catch (SQLException e) {
            log.error("【数据对比】-比对表:{}-查询异常:{}", tableContrastParam.getTableFlag(), e.getMessage(), e);
            throw new RuntimeException("源表明细数据查询异常");
        }
    }
}
